<template>
  <div>
    <el-button type="primary" round size="small" @click="addVacc"
      >添加疫苗</el-button
    >

    <div :style="{ 'margin-top': '25px' }">
      <el-card>
        <el-table :data="tableList" style="width: 100%">
          <el-table-column prop="vaccineName" label="疫苗名称" align="center">
          </el-table-column>
          <el-table-column prop="surplusNum" label="库存量" align="center">
          </el-table-column>
          <el-table-column
            prop="appointmentNum"
            label="可预约量"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="address" label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                v-show="scope.row.state == 0"
                size="mini"
                type="primary"
                @click="handleEdit(scope.row)"
                >修改</el-button
              >
              <el-button
                v-show="scope.row.state == 0"
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >下架</el-button
              >
              <el-button
                size="mini"
                @click="handleOn(scope.row)"
                v-show="scope.row.state == 1"
                >恢复上架</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增或者修改疫苗弹窗 -->
      <el-dialog
        :title="title"
        :visible="visible"
        size="small"
        @close="close"
        width="465px"
      >
        <el-form
          :model="form"
          inline
          label-position="right"
          label-width="85px"
          ref="formRef"
          :rules="rules"
        >
          <el-form-item :label="label" prop="data">
            <el-input v-model="form.data" style="width:276px"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="confirm">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 表格数据
      tableData: { pageNum: 1, pageSize: 50 },
      tableList: {},
      title: "",
      label: "",
      visible: false,
      form: { data: "" },
      vaccineId: "",
      rules: {
        data: [
          {
            required: true,
            message: "疫苗名称不能为空",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    // // 重置表单
    // resetLoginForm() {
    //   this.$refs.loginFormRef.resetFields();
    // },
    async getTablelist() {
      const { data: res } = await this.$http.post(
        "/vaccine/getVaccinePage",
        this.tableData
      );
      if (res.code == 200) {
        this.tableList = res.data.data;
        console.log(this.tableList);
      } else {
        this.$message.error(res.msg);
      }
    },
    handleEdit(data) {
      this.visible = true;
      this.title = "修改可预约量";
      this.label = "可预约量:";
      this.form.data = data.appointmentNum;
      this.vaccineId = data.vaccineId;
    },
    addVacc() {
      this.title = "新增疫苗";
      this.label = "疫苗名称:";
      this.visible = true;
    },
    close() {
      this.visible = false;
      this.form.data = "";
    },
    confirm() {
      if (this.title == "新增疫苗") {
        this.$refs.formRef.validate(value => {
          if (!value) return;
          let vaccineName = this.form.data;
          this.$http
            .post("/vaccine/insertVaccine", {
              vaccineName: vaccineName
            })
            .then(res => {
              if (res.data.code == 200) {
                this.getTablelist();
                this.$message.success(res.msg);
              } else {
                this.$message.error(res.msg);
              }
            });
        });
      }
      // 修改疫苗预约量
      else {
        this.$refs.formRef.validate(value => {
          if (!value) return;
          this.$http
            .post("/vaccine/updateAppoinmentNumById", {
              appointmentNum: this.form.data,
              vaccineId: this.vaccineId
            })
            .then(res => {
              console.log(res);
              if (res.data.code == 200) {
                this.close();
                this.getTablelist();
                this.$message.success(res.msg);
              } else {
                this.$message.error(res.msg);
              }
            });
        });
      }
    },
    // 下架
    handleDelete(e) {
      this.$confirm("是否下架疫苗：" + e.vaccineName + "?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http
            .post("/vaccine/updateById", {
              state: 1,
              vaccineId: e.vaccineId
            })
            .then(res => {
              console.log(res.data.code);
              if (res.data.code == 200) {
                this.getTablelist();
                this.$message({
                  type: "success",
                  message: "下架成功!"
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消下架"
          });
        });
    },
    // 上架
    handleOn(e) {
      this.$confirm("是否恢复疫苗：" + e.vaccineName + "上架?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http
            .post("/vaccine/updateById", {
              state: 0,
              vaccineId: e.vaccineId
            })
            .then(res => {
              console.log(res.data.code);
              if (res.data.code == 200) {
                this.getTablelist();
                this.$message({
                  type: "success",
                  message: "上架成功!"
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消上架"
          });
        });
    }
  },
  mounted() {
    this.getTablelist();
  }
};
</script>

<style lang="less" scoped></style>
